﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using KL.UFrame.Api.Extensions;
using KL.UFrame.Core.Result;
using KL.UFrame.Data.Business;
using KL.UFrame.Data.Models;

namespace KL.UFrame.Api.ApiControllers
{
    [RoutePrefix("api/Config")]
    public class ConfigController : K9ApiBaseController
    {
        private IAppConfigBusiness _business;
        public ConfigController(IAppConfigBusiness business)
        {
            _business = business;
        }

        public async Task<ResultMessage<List<AppConfigModel>>> Get()
        {
            var result =await _business.GetAllAsync();
            return new ResultMessage<List<AppConfigModel>>(result);
        }

        public async Task<ResultMessage> Post(AppConfigModel model)
        {
            if (!ModelState.IsValid)
            {
                return ModelState.ToResultMessage();
            }

            var result = await _business.UpdateContentAsync(model.Key, model.Content);

            return result ? new ResultMessage() : new ResultMessage(-500, "保存数据失败，请稍后再试");
        }
    }
}
